package org.or5e.plugin.core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.bind.JAXBElement;

import org.or5e.core.BaseObject;
import org.or5e.plugin.core.vo.ITunesPlaylist;
import org.or5e.plugin.core.vo.ITunesTrack;
import org.or5e.plugin.pojo.itunes.Array;
import org.or5e.plugin.pojo.itunes.Dict;
import org.or5e.plugin.pojo.itunes.Plist;

public class ITunesHelper extends BaseObject {
	private static ITunesHelper _itunesHelper = null;;
	private List<String> dontAddToPlaylist = new ArrayList<String>();
	private Map<String, ITunesTrack> trackMap = new HashMap<>();
	private Map<String, ITunesPlaylist> playlistMap = new HashMap<>();
	private ITunesFileCopy fileCopy = new ITunesFileCopy();
	static {
		_itunesHelper = new ITunesHelper();
	}
	private ITunesHelper() {
		dontAddToPlaylist.add("Library");
		dontAddToPlaylist.add("Music");
		dontAddToPlaylist.add("Movies");
		dontAddToPlaylist.add("TV Shows");
		dontAddToPlaylist.add("Podcasts");
		dontAddToPlaylist.add("Genius");
		dontAddToPlaylist.add("90’s Music");
		dontAddToPlaylist.add("Classical Music");
		dontAddToPlaylist.add("My Top Rated");
		dontAddToPlaylist.add("Recently Added");
		dontAddToPlaylist.add("Recently Played");
		dontAddToPlaylist.add("Top 25 Most Played");
	}
	public static ITunesHelper getITunesHelper() {
		return _itunesHelper ;
	}
	@SuppressWarnings("rawtypes")
	public void parseAndLoadTrackDetails(Plist itunesList) {
		List<Object> dictOrArrayOrData = itunesList.getDict().getDictOrArrayOrData();
		int index = 1;
		for (Object object : dictOrArrayOrData) {
			if(object instanceof Dict) {
				Dict itunesDictList = (Dict) object;
				List<Object> dictList = itunesDictList.getDictOrArrayOrData();
				for (Object keyDictObject : dictList) {
					if(keyDictObject instanceof Dict) {
						ITunesTrack track = new ITunesTrack();
						track.loadInfo((Dict)keyDictObject);

						trackMap.put(track.getTrackID(), track);
						if(track.copyFile()) {
							fileCopy.copyFile(track, (index++)+"_");
						}
					}
				}
			}
			else if (object instanceof Array) {
				Array itunesPlaylist = (Array) object;
				List<Dict> itunesPlaylistDictList = itunesPlaylist.getDict();
				for (Dict itunesPlaylistDict : itunesPlaylistDictList) {
					List<Object> itunesPlaylistDictData = itunesPlaylistDict.getDictOrArrayOrData();
					String playListName = (String)((JAXBElement)itunesPlaylistDictData.get(1)).getValue();
					
					if(! dontAddToPlaylist.contains(playListName)) {
						String playListID = String.valueOf(((JAXBElement)itunesPlaylistDictData.get(3)).getValue());
						String persistancePlayListID = (String)((JAXBElement)itunesPlaylistDictData.get(5)).getValue();
						ITunesPlaylist playlist = new ITunesPlaylist();
						playlist.setPlaylistID(playListID);
						playlist.setPlayListName(playListName);
						playlist.setPlaylistPersistanceID(persistancePlayListID);
						for (Object findArray : itunesPlaylistDictData) {
							if(findArray instanceof Array) {
								Array trackList = (Array) findArray;
								playlist.addToTrackIDList(trackList);
							}
						}
						playlistMap.put(playListName, playlist);
					}
				}
			}
		}
		debug("Total MapSize: "+trackMap.size());
		debug("Total Playlist: "+playlistMap.size());
	}
	@Override public String getName() {
		return "ITunesHelper";
	}
	public Map<String, ITunesTrack> getTrackMap() {
		return trackMap;
	}
	public void setTrackMap(Map<String, ITunesTrack> trackMap) {
		this.trackMap = trackMap;
	}
	public Map<String, ITunesPlaylist> getPlaylistMap() {
		return playlistMap;
	}
	public void setPlaylistMap(Map<String, ITunesPlaylist> playlistMap) {
		this.playlistMap = playlistMap;
	}
}